import datetime
from cs_weixinapp.models import  xiangmu_user, xiangmu, xiangmu_chanyu

# 创建序列化器
# class HiolabsCategorySerializer(serializers.ModelSerializer):
#     class Meta:
#         model = HiolabsCategory
#         fields = ['id', 'icon_url', 'name', 'sort_order']  # 指定要序列化的字段

# serializers.py
from rest_framework import serializers




class xiangmuSerializer(serializers.ModelSerializer):
    class Meta:
        model = xiangmu
        fields = '__all__'




class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = xiangmu_user
        fields = '__all__'

class daSerializer(serializers.ModelSerializer):
    class Meta:
        model = xiangmu_user
        fields = ('id', 'user_img_url', 'user_name', 'loging_time', )

class xiangmu_chanyuSerializer(serializers.ModelSerializer):
    class Meta:
        model = xiangmu_chanyu
        fields = '__all__'


class CommentSerializer(serializers.ModelSerializer):
    class Meta:
        model = xiangmu
        fields = '__all__'

    def to_representation(self, instance):
        data = super().to_representation(instance)
        # data['create_time']获取到数据库里存储的时间戳,然后用localtime()函数转换为localtime

        dt_object = datetime.datetime.fromtimestamp(data['time'])
        data['time'] = dt_object.strftime('%Y-%m-%d %H:%M:%S')
        # 返回处理之后的数据
        return data

#
# class GoodsSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = HiolabsGoods
#         fields = ('name', 'id', 'goods_brief', 'min_retail_price', 'list_pic_url', 'goods_number')
#
#
# class OrderSerializer(serializers.ModelSerializer):
#     # goodsList = OrderGoodsSerializer(source='ordergoods_set', many=True, read_only=True)
#     # goodsCount = serializers.SerializerMethodField()
#     # # add_time = serializers.DateTimeField(source='creation_time')
#     # # add_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
#     # order_status_text = serializers.SerializerMethodField()
#     # handleOption = serializers.SerializerMethodField()
#
#     class Meta:
#         model = HiolabsOrder
#         fields = '__all__'
#
#     def to_representation(self, instance):
#         data = super().to_representation(instance)
#         # data['create_time']获取到数据库里存储的时间戳,然后用localtime()函数转换为localtime
#
#         dt_object = datetime.datetime.fromtimestamp(data['add_time'])
#         data['add_time'] = dt_object.strftime('%Y-%m-%d %H:%M:%S')
#         # 返回处理之后的数据
#         return data
#     # def get_goodsCount(self, obj):
#     #     return obj.ordergoods_set.aggregate(total=models.Sum('number'))['total'] or 0
#     #
#     # def get_order_status_text(self, obj):
#     #     # 这里假设你有一个方法来获取订单状态文本
#     #     # 你需要根据实际逻辑来实现这个方法
#     #     return get_order_status_text_from_somewhere(obj.order_status)
#     #
#     # def get_handleOption(self, obj):
#     #     # 这里假设你有一个方法来获取可操作的选项
#     #     # 你需要根据实际逻辑来实现这个方法
#     #     return get_order_handle_options_from_somewhere(obj.id)
#
